package kr.co.handysoft.smartPolice.testcase;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.zip.DataFormatException;

import kr.co.handysoft.common.compress.Archiving;
import kr.co.handysoft.common.server.CommunicationData;

import net.sf.json.JSONObject;

public class ControlServerTestCase {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	
		try {
			Socket socket = new Socket("localhost", 9991);
			
			if (socket.isConnected()){
				OutputStream os_socket;
				try {
					os_socket = socket.getOutputStream();
					InputStream is_socket = socket.getInputStream();
					
					BufferedWriter bufferW = new BufferedWriter(new OutputStreamWriter(os_socket));
					BufferedReader bufferR = new BufferedReader(new InputStreamReader(is_socket));
					bufferW.write("ALL\r\n");
					bufferW.flush(); 
					
					String str = bufferR.readLine();
					JSONObject jObj = JSONObject.fromObject(str);
					CommunicationData cData = (CommunicationData)JSONObject.toBean(jObj, CommunicationData.class);
					
					System.out.println("cData : " + cData.toJSON());
					System.out.println("cData.command : " + cData.getCommand());
					System.out.println("cData.length : " + cData.getLength());
					System.out.println("cData.md5 : " + cData.getMd5());
					System.out.println("cData.body : " + Archiving.get(cData.toJSON(), "body"));
					System.out.println("cData.body.disk : " + Archiving.get(Archiving.get(cData.toJSON(), "body"), "disk"));
					System.out.println("cData.body.memory : " + Archiving.get(Archiving.get(cData.toJSON(), "body"), "memory"));
					System.out.println("cData.body.cpu : " + Archiving.get(Archiving.get(cData.toJSON(), "body"), "cpu"));
					System.out.println("cData.body.env : " + Archiving.get(Archiving.get(cData.toJSON(), "body"), "env"));

				} catch (IOException e) {
					e.printStackTrace();
				}

			}			
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
